// pages/index/newsDetail/newsDetail.js
var app = getApp(),that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsDetail: {},
    textContext: '',
    context: '',
    url: '../../../../image/huiStar.png',
    hoverUrl: '../../../../image/redStar.png',
    memderId:'',
    giveUpStatus:'',
    paise:'',
    isshow:false,
    status:'',
    zanzan:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(JSON.parse(decodeURIComponent(options.item)))
    that = this;
    // this.giveUp();
    this.setData({
      newsDetail: JSON.parse(decodeURIComponent(options.item)),
      memderId: wx.getStorageSync('bizUserInfo').id,
      status: JSON.parse(decodeURIComponent(options.item)).giveUpStatus
    })
    this.data.textContext = this.data.newsDetail.articlecontent

    // Yueduliang
    this.setReading()
    console.log(JSON.stringify(decodeURIComponent(options.item)))
  },
  setReading: function() {
    console.log("fad" + this.data.newsDetail.treasureId)
    app.getData({
      url: app.api.getReading(this.data.newsDetail.treasureId,this.data.memderId, 1),
      method: "POST",
      ok: function(res) {
        console.log(res)

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var reg = new RegExp('<img', "g")
    this.setData({
      context: this.data.textContext.replace(reg, '<img style="max-width:100%;height:auto"'),
      memderId: wx.getStorageSync('bizUserInfo').id,
      paise: this.data.newsDetail.giveUpStatus
    });
    if (this.data.status == '' || this.data.status == null || this.data.status == 1){
      this.setData({
        zanzan: 3
      })
    }else{
      this.setData({
        zanzan: 2
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    this.shareModalClose()
  },
  showShareModal: function() {
    this.setData({
      share_modal_active: "active",
      no_scroll: !1
    });
  },
  shareModalClose: function() {
    this.setData({
      share_modal_active: "",
      no_scroll: !1
    });
  },
  //点赞
  giveUp(e) {
    console.log(e)
    console.log(this.data.newsDetail.giveUpStatus)
    var zan=3
    if (this.data.newsDetail.giveUpStatus == null || this.data.newsDetail.giveUpStatus==1 ||this.data.newsDetail.giveUpStatus==3){
      zan=2
    }else{
      zan=3
    }
    this.isPaise(zan)
  },
  isPaise(type){
    app.getData({
      url: app.api.getReading(this.data.newsDetail.treasureId, this.data.memderId, type),
      method: 'POST',
      ok: function (res) {
        console.log(res)
        if (res.errMsg == 'request:ok' && res.statusCode == 200) {
          if (res.data.code == 1) {
            console.log()
            if (type == 2) {

              wx.showToast({
                title: '点赞成功！',
                icon:'none'
              })
              that.data.newsDetail.giveUpStatus = 2;
              console.log('f' + that.data.newsDetail.giveUpStatus)
              // that.setData({
              //   zanzan: that.data.newsDetail.giveUpStatus
              // })
            } else {
              wx.showToast({
                title: '取消点赞成功！',
                icon: 'none'
              })
              that.data.newsDetail.giveUpStatus = 3;
              console.log('f' + that.data.newsDetail.giveUpStatus)

            }
            that.setData({
              zanzan: that.data.newsDetail.giveUpStatus
            })
          } else {
            wx.showToast({
              title: '操作失败！',
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: res.data.errMsg
          })
        }
      }
    })
  }
})